/**
 * 模型中心 - 模型版本列表-查看
 */
import React, { Component, createContext } from 'react'
import { Modal, Tooltip, Table, Descriptions, Tabs } from 'antd';
// marked
import { viewVersion, } from '@/services/modelCenter';
// import { viewVersion, } from '@/services/modelCenter1';
import Mark from '../../mark/components/markContent';

const { TabPane } = Tabs;
class View extends Component {
    constructor(props) {
        super(props);
        this.state = {
            data: {},
            // 实体算法字段
            entityDes: [],
            // 关系/属性算法字段
            des: [],
            // 当前激活pane
            activeKey: 'entity',
            // 模型类型
            modelType: null
        };
    }

    componentDidMount() {
        const { versionId } = this.props;
        Boolean(versionId) && viewVersion(versionId).then(res => {
            if (res.retCode === '0') {
                const modelType = res.data.modelVersionVO.modelType;
                // 实体词向量
                const anotherWordvecId = res.data.modelVersionVO.anotherWordvecId;
                // 关系/属性词向量
                const wordvecId = res.data.modelVersionVO.wordvecId;

                const dataName = {
                    dim: '词维度',
                    window: '窗口',
                    minCount: '单词最小词频',
                    negativeUsed: '是否采用负采样',
                    negativeSampleNum: '负采样数量',
                    iterNum: '迭代次数',
                    wordvecName: '词向量表',
                };
                let entityDes = [];
                let des = [];
                let entityData;
                let dataInfo;
                if (modelType !== 1 && modelType !== 4 && modelType !== 5) {
                    entityData = res.data.modelWordvecVO[0] && res.data.modelWordvecVO[0].wordvecConfig;
                } else {
                    if(anotherWordvecId){
                        entityData = res.data.modelWordvecVO.filter(item => item.wordvecId === anotherWordvecId).length>0 ? res.data.modelWordvecVO.filter(item => item.wordvecId === anotherWordvecId)[0].wordvecConfig : {};
                    }
                    let info = res.data.modelWordvecVO.filter(item => item.wordvecId === wordvecId);
                    dataInfo = info && info.length > 0 && info[0].wordvecConfig;
                    for (let k in dataInfo) {
                        if (k !== 'sourceType' && k !== 'algorithm_id') {
                            if (k === 'negativeUsed') {
                                des.push({ label: dataName[k], val: ['否', '是'][dataInfo[k] - 0] })
                            } else {
                                des.push({ label: dataName[k], val: dataInfo[k] })
                            }
                        }
                    }
                }

                for (let k in entityData) {
                    if (k !== 'sourceType' && k !== 'algorithm_id') {
                        if (k === 'negativeUsed') {
                            entityDes.push({ label: dataName[k], val: ['否', '是'][entityData[k] - 0] })
                        } else {
                            entityDes.push({ label: dataName[k], val: entityData[k] })
                        }
                    }
                }
                this.setState({ data: res.data, entityDes, des, modelType })
            }
        })
    }

    infoToParent(key) {
        this.props.callBack(key);
    }


    render() {
        const { isShowViewModel, } = this.props;
        const { data, des, entityDes, activeKey, modelType } = this.state;
        const { modelInfoVO, modelVersionVO, dataSetInfoVOS } = data;
        const columns = [
            { title: '字段', dataIndex: 'key', key: 'name' },
            {
                title: '准确率', dataIndex: 'precision', key: 'precision', render: record => {
                    return String(record).slice(0, 4)
                }
            },
            {
                title: '召回率', dataIndex: 'recall', key: 'recall', render: record => {
                    return String(record).slice(0, 4)
                }
            },
            {
                title: 'F1值', dataIndex: 'F1', key: 'F1', render: record => {
                    return String(record).slice(0, 4)
                }
            },
        ];
        let segmentNum = 0;
        dataSetInfoVOS && dataSetInfoVOS.map(val => {
            segmentNum += val.segmentNum - 0
        });
        return (
            <Modal
                title={'模型详情'}
                maskClosable={false}
                width={600}
                visible={isShowViewModel}
                onCancel={this.infoToParent.bind(this, false)}
                footer={null}
            >
                {modelType === 1 || modelType === 4 || modelType === 5? <Tabs defaultActiveKey={'entity'}>
                    <TabPane key={'entity'} tab={modelType === 5 ? '触发词算法':'实体算法'}>
                        <div id='dataSets'>
                            <Descriptions column={1}>
                                <Descriptions.Item label="模型ID">{modelInfoVO && modelInfoVO.modelId}</Descriptions.Item>
                                <Descriptions.Item label="模型算法">{modelInfoVO && modelInfoVO.anotherModelAlgorithm}</Descriptions.Item>
                                <Descriptions.Item label="模型名称">{modelInfoVO && modelInfoVO.modelName}</Descriptions.Item>
                                <Descriptions.Item label="模型状态">{['未发布', '已发布'][modelInfoVO && modelInfoVO.modelStatus - 0]}</Descriptions.Item>
                                <Descriptions.Item label="更新时间">{modelInfoVO && modelInfoVO.updateTimeShort}</Descriptions.Item>
                                <Descriptions.Item label="训练数据集">
                                    {
                                        dataSetInfoVOS && dataSetInfoVOS.map((val, i) => {
                                            if (i === dataSetInfoVOS.length - 1) {
                                                return `${val.dataSetName} `
                                            } else {
                                                return `${val.dataSetName} , `
                                            }
                                        })
                                    }
                                </Descriptions.Item>
                                <Descriptions.Item label="训练题量"> {segmentNum} </Descriptions.Item>
                                {
                                    entityDes.map((v, i) => <Descriptions.Item key={i} label={v.label}>{v.val}</Descriptions.Item>)
                                }
                                <Descriptions.Item label={modelType === 5 ? '触发词模型评估':'实体模型评估'} className='table'>
                                    <div style={{ width: 420, }}><Table columns={columns} dataSource={modelVersionVO.anotherModelStatistic && JSON.parse(modelVersionVO.anotherModelStatistic || "[]")} pagination={false} size='small' /></div>
                                </Descriptions.Item>

                            </Descriptions>
                        </div>
                    </TabPane>
                    <TabPane key={'relation'} tab={modelType === 1 ? '关系算法' : (modelType === 5 ? '元素算法':'属性算法')}>
                        <div id='dataSets'>
                            <Descriptions column={1}>
                                <Descriptions.Item label="模型ID">{modelInfoVO && modelInfoVO.modelId}</Descriptions.Item>
                                <Descriptions.Item label="模型算法">{modelInfoVO && modelInfoVO.modelAlgorithm}</Descriptions.Item>
                                <Descriptions.Item label="模型名称">{modelInfoVO && modelInfoVO.modelName}</Descriptions.Item>
                                <Descriptions.Item label="模型状态">{['未发布', '已发布'][modelInfoVO && modelInfoVO.modelStatus - 0]}</Descriptions.Item>
                                <Descriptions.Item label="更新时间">{modelInfoVO && modelInfoVO.updateTimeShort}</Descriptions.Item>
                                <Descriptions.Item label="训练数据集">
                                    {
                                        dataSetInfoVOS && dataSetInfoVOS.map((val, i) => {
                                            if (i === dataSetInfoVOS.length - 1) {
                                                return `${val.dataSetName} `
                                            } else {
                                                return `${val.dataSetName} , `
                                            }
                                        })
                                    }
                                </Descriptions.Item>
                                <Descriptions.Item label="训练题量"> {segmentNum} </Descriptions.Item>
                                {
                                    des.length>0 && des.map((v, i) => <Descriptions.Item key={i} label={v.label}>{v.val}</Descriptions.Item>)
                                }
                                <Descriptions.Item label={modelType === 1 ? '关系模型评估' : (modelType === 5 ? '元素模型评估':'属性模型评估')} className='table'>
                                    <div style={{ width: 420, }}><Table columns={columns} dataSource={modelVersionVO && JSON.parse(modelVersionVO.modelStatistic || "[]")} pagination={false} size='small' /></div>
                                </Descriptions.Item>

                            </Descriptions>
                        </div>
                    </TabPane>
                </Tabs> :
                    <div id='dataSets'>
                        <Descriptions column={1}>
                            <Descriptions.Item label="模型算法">{modelInfoVO && modelInfoVO.modelAlgorithm}</Descriptions.Item>
                            <Descriptions.Item label="训练数据集">
                                {
                                    dataSetInfoVOS && dataSetInfoVOS.map((val, i) => {
                                        if (i === dataSetInfoVOS.length - 1) {
                                            return `${val.dataSetName} `
                                        } else {
                                            return `${val.dataSetName} , `
                                        }
                                    })
                                }
                            </Descriptions.Item>
                            <Descriptions.Item label="训练题量"> {segmentNum} </Descriptions.Item>
                            {
                                entityDes.map((v, i) => <Descriptions.Item key={i} label={v.label}>{v.val}</Descriptions.Item>)
                            }
                            <Descriptions.Item label={modelType === 0?'实体模型评估':"模型评估"} className='table'>
                                <div style={{ width: 420, }}><Table columns={columns} dataSource={modelVersionVO && JSON.parse(modelVersionVO.modelStatistic || "[]")} pagination={false} size='small' /></div>
                            </Descriptions.Item>

                        </Descriptions>
                    </div>}
            </Modal>
        )
    }
}

export default View
